home *** CD-ROM | disk | FTP | other *** search
- /*
- File: DataItem.h
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __DATAITEM__
- #define __DATAITEM__
-
- #ifndef __MEMORY__
- #include "Memory.h"
- #endif
-
- #ifndef __DIRECTOBJECT__
- #include "DirectObject.h"
- #endif
-
- #ifndef __APPLEEVENTS__
- // For DescType and typedefs for data types
- #include <AppleEvents.h>
- #endif
-
- /***********************************|****************************************/
-
- #pragma push
- #pragma segment DataItem
-
- class ostream;
- class ADataItem;
-
- extern short CompareDataItems ( const void* a, unsigned long alen, const void* b, unsigned long blen );
-
- /*============================================================================
-
- ADataItem is a abstract class representing a DataItem, a chunk of
- contiguous RAM which has a physical length and a starting address
- and a 'type'.
-
- ----------------------------------------------------------------------------*/
-
- class ADataItem : public TDirectObject
- {
- public:
-
- virtual ~ADataItem ();
- ADataItem& operator = ( const ADataItem& );
-
- // Conversion operators, which allow simple assignments to be made to
- // a DataItem like:
- // ADataItem x;
- // x = 7; // x.GetDataType() == typeLongint, * ( long ) x.GetPhysicalStart() = 8
- // x = "Hi!"; // x.GetDataType() == typeText; x.GetPhysicalStart() = "Hi!";
- // et al.
- //
- virtual ADataItem& operator = ( const long );
- virtual ADataItem& operator = ( const char* );
- virtual ADataItem& operator = ( const StringPtr );
-
-
- virtual operator long () const;
- virtual operator Str255& () const;
-
- operator const void* () const;
-
- // subclasses must implement these…
- virtual const void* GetPhysicalStart () const = 0;
- virtual unsigned long SetPhysicalLength ( unsigned long ) = 0;
- virtual unsigned long GetPhysicalLength () const = 0;
- virtual DescType GetDataType() const = 0;
- virtual void SetDataType(DescType dataType) = 0;
-
- // comparison methods
- virtual Boolean operator == ( const ADataItem& ) const;
- virtual Boolean operator != ( const ADataItem& ) const;
-
- // safe copying methods
- virtual unsigned long ReadFrom ( const void* source, unsigned long length, DescType dataType);
- virtual unsigned long WriteTo ( void* dest, unsigned long length ) const;
-
- // content setting methods
- virtual void ZeroDataItem ();
- virtual void FillDataItem ( unsigned char );
-
- // debugging
- virtual ostream& operator >> ( ostream& ) const;
-
- protected: ADataItem ();
- };
-
- /*============================================================================
-
- CDataItem is a concrete class representing a DataItem.
-
- The implementation of this class is smart; i.e. if the DataItem logical
- length is no greater than 7 bytes, then the DataItem data is stored
- inside the object, that keeps us from senselessly allocating teensy
- blocks on the heap since we have space in the object already.
-
- ----------------------------------------------------------------------------*/
-
- class CDataItem : public ADataItem
- {
- public: CDataItem ();
- CDataItem ( unsigned long length, DescType dataType = typeWildCard );
- CDataItem ( const void* source, unsigned long length, DescType dataType = typeWildCard );
- CDataItem ( const ADataItem& );
- CDataItem ( const CDataItem& );
- virtual ~CDataItem ();
- CDataItem& operator = ( const CDataItem& );
-
- virtual const void* GetPhysicalStart () const;
- virtual unsigned long SetPhysicalLength ( unsigned long );
- virtual unsigned long GetPhysicalLength () const;
-
- virtual DescType GetDataType() const;
- virtual void SetDataType(DescType dataType);
-
- virtual ostream& operator >> ( ostream& ) const;
-
- private:
- unsigned char fIsExternal;
- DescType fDataType;
-
- struct DataItemExternal
- {
- unsigned long fLength;
- void* fDataItem;
- };
-
- #define kDataItemMaxInternalLength ( sizeof ( DataItemExternal ) - sizeof ( unsigned char ) )
-
- struct DataItemInternal
- {
- unsigned char fLength;
- unsigned char fDataItem [ kDataItemMaxInternalLength ];
- };
-
- union
- {
- DataItemExternal fExternal;
- DataItemInternal fInternal;
- } fType;
- };
-
- /*============================================================================
-
- CPrefixDataItem is a concrete class which represents a DataItem
- which creates a new DataItem containing a length prefix from an existing
- DataItem. The length prefix can be a 1, 2, or 4 byte unsigned integer.
-
- The length prefix DataItem will be larger than the original DataItem by
- either 1, 2, or 4 bytes, and this prefix will contain an unsigned
- integer representing the length of the original DataItem.
-
- When the length DataItem is destructed, it can also write its contents
- back to the original DataItem, this option is selected in the constructor
- argument list parameter “updateSource”.
-
- For example, this class can be used to easily create a pascal string
- copy of a null terminated string:
-
- char s [] = "Hello World!!";
- CPrefixDataItem b ( s, strlen ( s ), sizeof ( char ) );
- char* t = (char*) b.GetPhysicalStart ();
-
- The data pointed to by <t> would look like:
-
- $0C $48 $65 $6c $6c $6f $20 $57 $6f $72 $6c $64 $21 $21
- ‘\n’ ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘ ’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ ‘!’ ‘!’
-
- Prefix buffers always have dataType == typeWildCard.
-
- ----------------------------------------------------------------------------*/
-
- class CPrefixDataItem : public ADataItem
- {
- public:
-
- enum DataItemPrefix { kByte = 1, kWord = 2, kLong = 4 };
-
- CPrefixDataItem ( const ADataItem&, DataItemPrefix = kByte );
- CPrefixDataItem ( ADataItem&, Boolean update, DataItemPrefix = kByte );
- CPrefixDataItem ( const void*, unsigned long, DataItemPrefix = kByte );
- CPrefixDataItem ( DataItemPrefix, unsigned long logicalSize );
- virtual ~CPrefixDataItem ();
-
- virtual const void* GetLogicalStart () const;
- virtual unsigned long SetLogicalLength ( unsigned long );
- virtual unsigned long GetLogicalLength () const;
-
- virtual const void* GetPhysicalStart () const;
- virtual unsigned long SetPhysicalLength ( unsigned long );
- virtual unsigned long GetPhysicalLength () const;
-
- virtual DescType GetDataType() const;
- virtual void SetDataType(DescType dataType);
-
- virtual void SetSource ( ADataItem* );
- const ADataItem* GetSource () const;
-
- virtual void SetUpdate ( Boolean );
- Boolean GetUpdate () const;
- virtual unsigned long UpdateNow ();
-
- virtual ostream& operator >> ( ostream& ) const;
-
- protected: void UpdatePrefixWithLength ( unsigned long logicalLength );
-
- private: CDataItem fDataItem;
- ADataItem* fSource;
- DataItemPrefix fPrefix;
- Boolean fUpdate;
- };
-
- /*============================================================================
-
- CWrapperDataItem is a concrete DataItem class which wraps a non-object
- chunk of nonrelocatable memory.
-
- ----------------------------------------------------------------------------*/
-
- class CWrapperDataItem : public ADataItem
- {
- public: CWrapperDataItem ( void* source, unsigned long length, DescType dataType = typeWildCard );
- virtual ~CWrapperDataItem ();
-
- virtual const void* GetPhysicalStart () const;
- virtual unsigned long SetPhysicalLength ( unsigned long );
- virtual unsigned long GetPhysicalLength () const;
-
- virtual DescType GetDataType() const;
- virtual void SetDataType(DescType dataType);
-
- private: void* fSource;
- unsigned long fLength;
- DescType fDataType;
- };
-
- /***********************************|****************************************/
-
- inline ADataItem::operator const void* () const { return GetPhysicalStart (); }
- inline void ADataItem::ZeroDataItem () { FillDataItem ( 0 ); }
- inline Boolean ADataItem::operator != ( const ADataItem& that ) const { return !operator == ( that ); }
-
- inline const void* CDataItem::GetPhysicalStart () const { return fIsExternal ? fType.fExternal.fDataItem : fType.fInternal.fDataItem; }
- inline unsigned long CDataItem::GetPhysicalLength () const { return fIsExternal ? fType.fExternal.fLength : fType.fInternal.fLength; }
- inline unsigned long CDataItem::GetDataType ( ) const { return fDataType; }
- inline void CDataItem::SetDataType ( DescType dataType ) { fDataType = dataType; }
-
- inline const void* CPrefixDataItem::GetPhysicalStart () const { return fDataItem.GetPhysicalStart (); }
- inline const void* CPrefixDataItem::GetLogicalStart () const { return (char*) fDataItem.GetPhysicalStart () + fPrefix; }
- inline unsigned long CPrefixDataItem::GetPhysicalLength () const { return fDataItem.GetPhysicalLength (); }
- inline unsigned long CPrefixDataItem::SetPhysicalLength ( unsigned long length ) { return fDataItem.SetPhysicalLength ( length ); }
- inline unsigned long CPrefixDataItem::GetDataType ( ) const { return typeWildCard; }
- inline void CPrefixDataItem::SetDataType ( DescType ) { }
- inline void CPrefixDataItem::SetSource ( ADataItem* source ) { fSource = source; }
- inline const ADataItem* CPrefixDataItem::GetSource () const { return fSource; }
- inline void CPrefixDataItem::SetUpdate ( Boolean update ) { fUpdate = update; }
- inline Boolean CPrefixDataItem::GetUpdate () const { return fUpdate; }
-
- inline const void* CWrapperDataItem::GetPhysicalStart () const { return fSource; }
- inline unsigned long CWrapperDataItem::GetPhysicalLength () const { return fLength; }
- inline unsigned long CWrapperDataItem::GetDataType ( ) const { return fDataType; }
- inline void CWrapperDataItem::SetDataType ( DescType dataType ) { fDataType = dataType; }
-
- /***********************************|****************************************/
-
- #pragma pop
-
- #endif // __DATAITEM__
-